package com.galen.manual.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.galen.manual.entity.FileRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文件记录Mapper接口
 */
@Mapper
public interface FileRecordMapper extends BaseMapper<FileRecord> {

    /**
     * 根据文件哈希查询记录
     * @param fileHash 文件哈希值
     * @return 文件记录
     */
    @Select("SELECT * FROM file_records WHERE file_hash = #{fileHash} AND deleted = 0 LIMIT 1")
    FileRecord selectByFileHash(@Param("fileHash") String fileHash);

    /**
     * 根据对象名称查询记录
     * @param objectName 对象名称
     * @return 文件记录
     */
    @Select("SELECT * FROM file_records WHERE object_name = #{objectName} AND deleted = 0 LIMIT 1")
    FileRecord selectByObjectName(@Param("objectName") String objectName);

    /**
     * 根据业务ID和类型查询文件列表
     * @param businessId 业务ID
     * @param businessType 业务类型
     * @return 文件记录列表
     */
    @Select("SELECT * FROM file_records WHERE business_id = #{businessId} AND business_type = #{businessType} AND deleted = 0 ORDER BY create_time DESC")
    List<FileRecord> selectByBusiness(@Param("businessId") Long businessId, @Param("businessType") String businessType);

    /**
     * 根据用户ID查询文件列表
     * @param uploadUserId 上传用户ID
     * @param limit 限制数量
     * @return 文件记录列表
     */
    @Select("SELECT * FROM file_records WHERE upload_user_id = #{uploadUserId} AND deleted = 0 ORDER BY create_time DESC LIMIT #{limit}")
    List<FileRecord> selectByUserId(@Param("uploadUserId") Long uploadUserId, @Param("limit") Integer limit);

    /**
     * 更新下载次数和最后下载时间
     * @param id 文件记录ID
     * @param downloadTime 下载时间
     * @return 更新结果
     */
    @Update("UPDATE file_records SET download_count = download_count + 1, last_download_time = #{downloadTime} WHERE id = #{id} AND deleted = 0")
    int updateDownloadCount(@Param("id") Long id, @Param("downloadTime") LocalDateTime downloadTime);

    /**
     * 更新文件状态
     * @param id 文件记录ID
     * @param status 新状态
     * @param errorMessage 错误信息（可选）
     * @return 更新结果
     */
    @Update("UPDATE file_records SET status = #{status}, error_message = #{errorMessage}, update_time = NOW() WHERE id = #{id}")
    int updateFileStatus(@Param("id") Long id, @Param("status") Integer status, @Param("errorMessage") String errorMessage);

    /**
     * 查询过期的文件记录
     * @param expireTime 过期时间
     * @return 过期文件记录列表
     */
    @Select("SELECT * FROM file_records WHERE expire_time IS NOT NULL AND expire_time < #{expireTime} AND deleted = 0")
    List<FileRecord> selectExpiredFiles(@Param("expireTime") LocalDateTime expireTime);

    /**
     * 根据文件分类查询文件列表
     * @param fileCategory 文件分类
     * @param limit 限制数量
     * @return 文件记录列表
     */
    @Select("SELECT * FROM file_records WHERE file_category = #{fileCategory} AND deleted = 0 ORDER BY create_time DESC LIMIT #{limit}")
    List<FileRecord> selectByCategory(@Param("fileCategory") String fileCategory, @Param("limit") Integer limit);

    /**
     * 统计用户上传文件总数
     * @param uploadUserId 上传用户ID
     * @return 文件总数
     */
    @Select("SELECT COUNT(*) FROM file_records WHERE upload_user_id = #{uploadUserId} AND deleted = 0")
    Integer countByUserId(@Param("uploadUserId") Long uploadUserId);

    /**
     * 统计分类文件总数
     * @param fileCategory 文件分类
     * @return 文件总数
     */
    @Select("SELECT COUNT(*) FROM file_records WHERE file_category = #{fileCategory} AND deleted = 0")
    Integer countByCategory(@Param("fileCategory") String fileCategory);

    /**
     * 统计文件总大小
     * @param fileCategory 文件分类（可选）
     * @return 文件总大小（字节）
     */
    @Select("SELECT COALESCE(SUM(file_size), 0) FROM file_records WHERE (#{fileCategory} IS NULL OR file_category = #{fileCategory}) AND deleted = 0")
    Long sumFileSize(@Param("fileCategory") String fileCategory);
}